#include "config_data.h"
#include "port.h"
#include "GUI.h"
#include "snapshot.h"

extern unsigned joypads[5];
#define joy joypads[0]

#define KBD_KEY_A 0x007
#define KBD_KEY_B 0x00F
#define KBD_KEY_X 0x006
#define KBD_KEY_Y 0x00E
#define KBD_KEY_RIGHT 0x008
#define KBD_KEY_LEFT 0x009
#define KBD_KEY_DOWN 0x00A
#define KBD_KEY_UP 0x00B
#define KBD_KEY_START 0x00C
#define KBD_KEY_SELECT 0x00D  
#define KBD_KEY_TL 0x005
#define KBD_KEY_TR 0x004 
#define KBD_SET(key)      ( joy |= (1<<key) )  
#define KBD_CLEAR(key)    ( joy &= (1<<~key) )

/* #define SNES_TR_MASK	    (1 << 4)
#define SNES_TL_MASK	    (1 << 5)
#define SNES_X_MASK	    (1 << 6)
#define SNES_A_MASK	    (1 << 7)
#define SNES_RIGHT_MASK	    (1 << 8)
#define SNES_LEFT_MASK	    (1 << 9)
#define SNES_DOWN_MASK	    (1 << 10)
#define SNES_UP_MASK	    (1 << 11)
#define SNES_START_MASK	    (1 << 12)
#define SNES_SELECT_MASK    (1 << 13)
#define SNES_Y_MASK	    (1 << 14)
#define SNES_B_MASK	    (1 << 15)
*/


extern void TurboFskip(void);
extern void QuickLayer(void);
extern int set_pause;
extern wchar_t game_filename[];
extern int spec_thing;

/*void QSaveInfo(int mode)
{
int stn;
char info[64];

if (mode) sprintf (info, "Game Saved to slot #%d", QSAVE_SLOT);
else sprintf (info, "Game Loaded from slot #%d", QSAVE_SLOT);   
stn = Str2ID ( info, 6, SID_ANY_LEN );
MessageBox(0x6fffffff, stn, 0, 1 , 1500, 0);
}*/

void QuickSave(wchar_t no, int mode)
{    
  wchar_t fname[256];
  wchar_t num[2];
  
  snwprintf(num, 2, L"%d", no);
  wstrcpy (fname, game_filename);
  
  wchar_t *s=wstrrchr(fname,'.');
  *++s=L'0'; *++s=L'0'; *++s=*num;
  
  set_pause=1;
  if (mode) {
    S9xFreezeGame (fname); 
    spec_thing=1;
    //QSaveInfo(1);
  }
  else {
    S9xUnfreezeGame (fname);
    spec_thing=2;
    //QSaveInfo(0);
  }
  set_pause=0;
  
}

//From mc_kibel:
//Here everything changes :D
//I don't know if it's good idea, however, some thousands lines of code are replaced by ~200.
//At the moment supporting only joypad mode, not function.

//Initializing with zero's, just in case 
int mode_table[24]    = { 0, };
int keycode_table[24] = { 0, };
int action_table[12]  = { 0, };
int key_table[24][12] = { 0, };

//As far as we are reading keycodes from dyn_const, we can not init global arrays.
//We need to create them in function, then copy.
int KeyControll_CreateArrays (void)
{
   int _key_table [24][12] = {
   {
     KUP_UP,     KUP_DOWN,      KUP_LEFT,      KUP_RIGHT,
     KUP_A,      KUP_B,         KUP_X,         KUP_Y,
     KUP_SEL,    KUP_STA,       KUP_L,         KUP_R
   },
   {
     KDOWN_UP,     KDOWN_DOWN,      KDOWN_LEFT,      KDOWN_RIGHT,
     KDOWN_A,      KDOWN_B,         KDOWN_X,         KDOWN_Y,
     KDOWN_SEL,    KDOWN_STA,       KDOWN_L,         KDOWN_R
   },
   {
     KLEFT_UP,     KLEFT_DOWN,      KLEFT_LEFT,      KLEFT_RIGHT,
     KLEFT_A,      KLEFT_B,         KLEFT_X,         KLEFT_Y,
     KLEFT_SEL,    KLEFT_STA,       KLEFT_L,         KLEFT_R
   },
   {
     KRIG_UP,     KRIG_DOWN,      KRIG_LEFT,      KRIG_RIGHT,
     KRIG_A,      KRIG_B,         KRIG_X,         KRIG_Y,
     KRIG_SEL,    KRIG_STA,       KRIG_L,         KRIG_R
   },
   {
     KENT_UP,     KENT_DOWN,      KENT_LEFT,      KENT_RIGHT,
     KENT_A,      KENT_B,         KENT_X,         KENT_Y,
     KENT_SEL,    KENT_STA,       KENT_L,         KENT_R
   },
   {
     KDEL_UP,     KDEL_DOWN,      KDEL_LEFT,      KDEL_RIGHT,
     KDEL_A,      KDEL_B,         KDEL_X,         KDEL_Y,
     KDEL_SEL,    KDEL_STA,       KDEL_L,         KDEL_R
   },
   {0,},{0,},{0,},{0,},{0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90_R},
   { KSTA_90_UP,KSTA_90_DOWN,KSTA_90_LEFT,KSTA_90_RIGHT,KSTA_90_A,KSTA_90_B,KSTA_90_X,KSTA_90_Y,KSTA_90_SEL,KSTA_90_STA,KSTA_90_L,KSTA_90_R},
   { KCAM_90_UP,KCAM_90_DOWN,KCAM_90_LEFT,KCAM_90_RIGHT,KCAM_90_A,KCAM_90_B,KCAM_90_X,KCAM_90_Y,KCAM_90_SEL,KCAM_90_STA,KCAM_90_L,KCAM_90_R},
   { KOP_90_UP,KOP_90_DOWN,KOP_90_LEFT,KOP_90_RIGHT,KOP_90_A,KOP_90_B,KOP_90_X,KOP_90_Y,KOP_90_SEL,KOP_90_STA,KOP_90_L,KOP_90_R},
   { KSPA_90_UP,KSPA_90_DOWN,KSPA_90_LEFT,KSPA_90_RIGHT,KSPA_90_A,KSPA_90_B,KSPA_90_X,KSPA_90_Y,KSPA_90_SEL,KSPA_90_STA,KSPA_90_L,KSPA_90_R},
   { KSPB_90_UP,KSPB_90_DOWN,KSPB_90_LEFT,KSPB_90_RIGHT,KSPB_90_A,KSPB_90_B,KSPB_90_X,KSPB_90_Y,KSPB_90_SEL,KSPB_90_STA,KSPB_90_L,KSPB_90_R},
   { K0_90_UP,K0_90_DOWN,K0_90_LEFT,K0_90_RIGHT,K0_90_A,K0_90_B,K0_90_X,K0_90_Y,K0_90_SEL,K0_90_STA,K0_90_L,K0_90_R},
   { K1_90_UP,K1_90_DOWN,K1_90_LEFT,K1_90_RIGHT,K1_90_A,K1_90_B,K1_90_X,K1_90_Y,K1_90_SEL,K1_90_STA,K1_90_L,K1_90_R},
   { K2_90_UP,K2_90_DOWN,K2_90_LEFT,K2_90_RIGHT,K2_90_A,K2_90_B,K2_90_X,K2_90_Y,K2_90_SEL,K2_90_STA,K2_90_L,K2_90_R},
   { K3_90_UP,K3_90_DOWN,K3_90_LEFT,K3_90_RIGHT,K3_90_A,K3_90_B,K3_90_X,K3_90_Y,K3_90_SEL,K3_90_STA,K3_90_L,K3_90_R},
   { K4_90_UP,K4_90_DOWN,K4_90_LEFT,K4_90_RIGHT,K4_90_A,K4_90_B,K4_90_X,K4_90_Y,K4_90_SEL,K4_90_STA,K4_90_L,K4_90_R},
   { K5_90_UP,K5_90_DOWN,K5_90_LEFT,K5_90_RIGHT,K5_90_A,K5_90_B,K5_90_X,K5_90_Y,K5_90_SEL,K5_90_STA,K5_90_L,K5_90_R},
   { K6_90_UP,K6_90_DOWN,K6_90_LEFT,K6_90_RIGHT,K6_90_A,K6_90_B,K6_90_X,K6_90_Y,K6_90_SEL,K6_90_STA,K6_90_L,K6_90_R},
   { K7_90_UP,K7_90_DOWN,K7_90_LEFT,K7_90_RIGHT,K7_90_A,K7_90_B,K7_90_X,K7_90_Y,K7_90_SEL,K7_90_STA,K7_90_L,K7_90_R},
   { K8_90_UP,K8_90_DOWN,K8_90_LEFT,K8_90_RIGHT,K8_90_A,K8_90_B,K8_90_X,K8_90_Y,K8_90_SEL,K8_90_STA,K8_90_L,K8_90_R},
   { K9_90_UP,K9_90_DOWN,K9_90_LEFT,K9_90_RIGHT,K9_90_A,K9_90_B,K9_90_X,K9_90_Y,K9_90_SEL,K9_90_STA,K9_90_L,K9_90_R}
  };
  int key_270_table [18][12] = {
   { KVUP_270_UP,KVUP_270_DOWN,KVUP_270_LEFT,KVUP_270_RIGHT,KVUP_270_A,KVUP_270_B,KVUP_270_X,KVUP_270_Y,KVUP_270_SEL,KVUP_270_STA,KVUP_270_L,KVUP_270_R},
   { KVDN_270_UP,KVDN_270_DOWN,KVDN_270_LEFT,KVDN_270_RIGHT,KVDN_270_A,KVDN_270_B,KVDN_270_X,KVDN_270_Y,KVDN_270_SEL,KVDN_270_STA,KVDN_270_L,KVDN_270_R},
   { KDIE_270_UP,KDIE_270_DOWN,KDIE_270_LEFT,KDIE_270_RIGHT,KDIE_270_A,KDIE_270_B,KDIE_270_X,KDIE_270_Y,KDIE_270_SEL,KDIE_270_STA,KDIE_270_L,KDIE_270_R},
   { KSTA_270_UP,KSTA_270_DOWN,KSTA_270_LEFT,KSTA_270_RIGHT,KSTA_270_A,KSTA_270_B,KSTA_270_X,KSTA_270_Y,KSTA_270_SEL,KSTA_270_STA,KSTA_270_L,KSTA_270_R},
   { KCAM_270_UP,KCAM_270_DOWN,KCAM_270_LEFT,KCAM_270_RIGHT,KCAM_270_A,KCAM_270_B,KCAM_270_X,KCAM_270_Y,KCAM_270_SEL,KCAM_270_STA,KCAM_270_L,KCAM_270_R},
   { KOP_270_UP,KOP_270_DOWN,KOP_270_LEFT,KOP_270_RIGHT,KOP_270_A,KOP_270_B,KOP_270_X,KOP_270_Y,KOP_270_SEL,KOP_270_STA,KOP_270_L,KOP_270_R},
   { KSPA_270_UP,KSPA_270_DOWN,KSPA_270_LEFT,KSPA_270_RIGHT,KSPA_270_A,KSPA_270_B,KSPA_270_X,KSPA_270_Y,KSPA_270_SEL,KSPA_270_STA,KSPA_270_L,KSPA_270_R},
   { KSPB_270_UP,KSPB_270_DOWN,KSPB_270_LEFT,KSPB_270_RIGHT,KSPB_270_A,KSPB_270_B,KSPB_270_X,KSPB_270_Y,KSPB_270_SEL,KSPB_270_STA,KSPB_270_L,KSPB_270_R},
   { K0_270_UP,K0_270_DOWN,K0_270_LEFT,K0_270_RIGHT,K0_270_A,K0_270_B,K0_270_X,K0_270_Y,K0_270_SEL,K0_270_STA,K0_270_L,K0_270_R},
   { K1_270_UP,K1_270_DOWN,K1_270_LEFT,K1_270_RIGHT,K1_270_A,K1_270_B,K1_270_X,K1_270_Y,K1_270_SEL,K1_270_STA,K1_270_L,K1_270_R},
   { K2_270_UP,K2_270_DOWN,K2_270_LEFT,K2_270_RIGHT,K2_270_A,K2_270_B,K2_270_X,K2_270_Y,K2_270_SEL,K2_270_STA,K2_270_L,K2_270_R},
   { K3_270_UP,K3_270_DOWN,K3_270_LEFT,K3_270_RIGHT,K3_270_A,K3_270_B,K3_270_X,K3_270_Y,K3_270_SEL,K3_270_STA,K3_270_L,K3_270_R},
   { K4_270_UP,K4_270_DOWN,K4_270_LEFT,K4_270_RIGHT,K4_270_A,K4_270_B,K4_270_X,K4_270_Y,K4_270_SEL,K4_270_STA,K4_270_L,K4_270_R},
   { K5_270_UP,K5_270_DOWN,K5_270_LEFT,K5_270_RIGHT,K5_270_A,K5_270_B,K5_270_X,K5_270_Y,K5_270_SEL,K5_270_STA,K5_270_L,K5_270_R},
   { K6_270_UP,K6_270_DOWN,K6_270_LEFT,K6_270_RIGHT,K6_270_A,K6_270_B,K6_270_X,K6_270_Y,K6_270_SEL,K6_270_STA,K6_270_L,K6_270_R},
   { K7_270_UP,K7_270_DOWN,K7_270_LEFT,K7_270_RIGHT,K7_270_A,K7_270_B,K7_270_X,K7_270_Y,K7_270_SEL,K7_270_STA,K7_270_L,K7_270_R},
   { K8_270_UP,K8_270_DOWN,K8_270_LEFT,K8_270_RIGHT,K8_270_A,K8_270_B,K8_270_X,K8_270_Y,K8_270_SEL,K8_270_STA,K8_270_L,K8_270_R},
   { K9_270_UP,K9_270_DOWN,K9_270_LEFT,K9_270_RIGHT,K9_270_A,K9_270_B,K9_270_X,K9_270_Y,K9_270_SEL,K9_270_STA,K9_270_L,K9_270_R}
  };
  int mode_0_table[19] = {KDEL_mode,KVUP_mode,KVDN_mode,KDIE_mode,KSTA_mode,KCAM_mode,KOP_mode,KSPA_mode,KSPB_mode,K0_mode,K1_mode,K2_mode,K3_mode,K4_mode,K5_mode,K6_mode,K7_mode,K8_mode,K9_mode};
  int mode_90_table[19] = {KDEL_90_mode,KVUP_90_mode,KVDN_90_mode,KDIE_90_mode,KSTA_90_mode,KCAM_90_mode,KOP_90_mode,KSPA_90_mode,KSPB_90_mode,K0_90_mode,K1_90_mode,K2_90_mode,K3_90_mode,K4_90_mode,K5_90_mode,K6_90_mode,K7_90_mode,K8_90_mode,K9_90_mode};
  int mode_270_table[19] = {KDEL_270_mode,KVUP_270_mode,KVDN_270_mode,KDIE_270_mode,KSTA_270_mode,KCAM_270_mode,KOP_270_mode,KSPA_270_mode,KSPB_270_mode,K0_270_mode,K1_270_mode,K2_270_mode,K3_270_mode,K4_270_mode,K5_270_mode,K6_270_mode,K7_270_mode,K8_270_mode,K9_270_mode};
  int _mode_table[24] = {KUP_mode,KDOWN_mode,KLEFT_mode,KRIG_mode,KENT_mode,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  int i,j;
  if (ROTATION == 0)
  {
    for (i = 0 ; i < 19 ; i++) _mode_table[i+5] = mode_0_table[i];
    for (i = 0 ; i < 18 ; i++)
    {
      for (j = 0 ; j < 12 ; j++) _key_table[i+6][j] = key_0_table[i][j];
    }
  }
  else if (ROTATION == 1)
  {
    for (i = 0 ; i < 19 ; i++) _mode_table[i+5] = mode_90_table[i];
    for (i = 0 ; i < 18 ; i++)
    {
      for (j = 0 ; j < 12 ; j++) _key_table[i+6][j] = key_90_table[i][j];
    }
  }
  else if (ROTATION == 2)
  {
    for (i = 0 ; i < 19 ; i++) _mode_table[i+5] = mode_270_table[i];
    for (i = 0 ; i < 18 ; i++)
    {
      for (j = 0 ; j < 12 ; j++) _key_table[i+6][j] = key_270_table[i][j];
    }
  }
  int _keycode_table[24] = {
    KEY_UP,     KEY_DOWN,     KEY_LEFT,     KEY_RIGHT,
    KEY_ENTER,  KEY_DEL,      KEY_VOL_UP,   KEY_VOL_DOWN,
    KEY_DIEZ,   KEY_STAR,     KEY_CAMERA_FOCUS,KEY_OPERATOR,
    KEY_GAME_A, KEY_GAME_B,   KEY_DIGITAL_0,KEY_DIGITAL_0+1,
    KEY_DIGITAL_0+2,KEY_DIGITAL_0+3,KEY_DIGITAL_0+4,KEY_DIGITAL_0+5,
    KEY_DIGITAL_0+6,KEY_DIGITAL_0+7,KEY_DIGITAL_0+8,KEY_DIGITAL_0+9
  };
  int _action_table [12] = {
    KBD_KEY_UP,      KBD_KEY_DOWN,     KBD_KEY_LEFT,     KBD_KEY_RIGHT,
    KBD_KEY_A,       KBD_KEY_B,        KBD_KEY_X,        KBD_KEY_Y,
    KBD_KEY_SELECT,  KBD_KEY_START,    KBD_KEY_TL,       KBD_KEY_TR
  };
  int res = 0;
  if (_keycode_table[10] == 0) _keycode_table[10] = KEY_CAMERA_SHOT;            //Fixing camera key for some phones, if there's no KEY_CAMERA_FOCUS, it will be replaced to KEY_CAMERA_SHOT.
  res += memcpy (mode_table,_mode_table,sizeof(mode_table));
  res += memcpy (keycode_table,_keycode_table,sizeof(keycode_table));
  res += memcpy (action_table,_action_table,sizeof(action_table));
  res += memcpy (key_table,_key_table,sizeof(key_table)); 
  return (res);
}
  
void onkey(int keycode, int type)
{ 
  char push;
  if (type == KBD_REPEAT || type == KBD_SHORT_PRESS || type == KBD_LONG_PRESS) push = 1;
  else push = 0;
  
  if (push == 1)
  {
    for (int i = 0 ; i < 24 ; i++)
    {
      if (keycode == keycode_table[i])
      {
        if (mode_table[i] == 1)
        {
          for (int j = 0 ; j < 12 ; j++)
          {
            if (key_table[i][j]) KBD_SET (action_table[j]);
          }
        }
      }
    }
  }
  else
  {
    for (int i = 0 ; i < 24 ; i++)
    {
      if (keycode == keycode_table[i])
      {
        for (int j = 0 ; j < 12 ; j++)
        {
          if (key_table[i][j]) KBD_CLEAR (action_table[j]);
        }
      }
    }
  }
  if ( keycode == KEY_RIGHT_SOFT)
  {
    set_pause=1;    
    GuiCreateGuiList(0, (BOOK*)MyBook, L"SEns9x 0.70", 7); 
  }
}

